package order

import (
	"fmt"
	"net/http"

	"gitee.com/zoneaurora/shop/internal/common"
	"gitee.com/zoneaurora/shop/internal/dto"
	"gitee.com/zoneaurora/shop/internal/logic/admin/base"
	"gitee.com/zoneaurora/shop/internal/model"
	"gitee.com/zoneaurora/shop/internal/vo"
	"github.com/gin-gonic/gin"
)

type OrderLogic struct {
	base.BaseLogic[model.Order]
}

func NewOrderLogic() *OrderLogic {
  return &OrderLogic{
    BaseLogic: base.BaseLogic[model.Order]{
      DB: common.DB(),
    },
  }
}

func (o *OrderLogic) Select(c *gin.Context) {
	//  获取请求参数
	id := c.Param("id")

	var order dto.OrderAndUser
  // 查询订单数据及关联的用户数据
  err :=o.DB.Table("orders").Where("orders.id = ?", id).
    Select(`orders.id, orders.user_id, orders.status, orders.transport_id,
    orders.telephone, orders.address, users.username`).
    Joins("JOIN users ON users.ID = orders.user_id").
    Scan(&order).Error
	if err != nil {
		common.JSON(c, http.StatusInternalServerError, "查询数据失败", nil)
		return
	}

  // 查询订单项具体信息
  var orderItems []dto.OrderItem
  err = o.DB.Table("order_items").
    Where("order_id = ?", order.ID).
    Select("order_items.id, products.name, order_items.number, products.img_url").
    Joins("JOIN products ON products.ID = order_items.product_id").
    Scan(&orderItems).Error
  if err != nil {
    common.JSON(c, http.StatusInternalServerError, "查询数据失败", nil)
    return
  }

  // 返回查询结果
	common.JSON(c, http.StatusOK, "查询数据成功", dto.OrderDto{
	    ID: order.ID,
	    Status: order.Status,
	    UserID: order.UserID,
	    TransportID: order.TransportID,
	    About: dto.OrderInfo{
	      Username: order.Username,
	      Telephone: order.Telephone,
	      Address: order.Address,
	      OrderItems: orderItems,
	    },
	  })
}

func (o *OrderLogic) Options(c *gin.Context) {
  // 获取用户数据
  var connectUsers []model.User
  if err := o.DB.Find(&connectUsers).Error; err != nil {
    common.JSON(c, http.StatusInternalServerError, "查询数据失败", nil)
  }
  var users []dto.SelectItem
  for _, v := range connectUsers{
    var item dto.SelectItem
    item.Label = v.Username
    item.Value = v.ID
    users = append(users, item)
  }

	var orderModel dto.OrderDto
	common.JSON(c, http.StatusOK, "查询数据成功", gin.H{
    "model": orderModel,
    "connection": gin.H{
      "UserID": users,
    },
  })
}

func (o *OrderLogic) GetPageList(c *gin.Context) {
	//  获取请求参数
	var requestParam vo.Pagination
	c.Bind(&requestParam)
	if requestParam.Order != "asc" {
		requestParam.Order = "desc"
	}
	if requestParam.SortName == "" {
		requestParam.SortName = "updated_at"
	}
	order := fmt.Sprintf("%s %s", requestParam.SortName, requestParam.Order)

	var models []model.Order
	var total int64
	res := o.DB.Not("status = 0").Find(&models)
	if res.Error != nil {
		common.JSON(c, http.StatusInternalServerError, "查询数据失败", nil)
		return
	}
	total = res.RowsAffected
	err := o.DB.Not("status = 0").Offset((requestParam.Page - 1) * requestParam.Size).
		Limit(requestParam.Size).Order(order).Find(&models).Error
	if err != nil {
		common.JSON(c, http.StatusInternalServerError, "查询数据失败", nil)
		return
	}
	common.JSON(c, http.StatusOK, "查询数据成功", gin.H{
		"list":  models,
		"total": total,
	})
}
